/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.controller;

/**
 *
 * @author Laurent WOUTERS
 */
public class Label extends Element {
    private xowl.gmi.model.cvssl.Label definition;
    private xowl.gmi.view.diagram.Label widget;
    
    public Label(Controller controller, Element parent, xowl.gmi.model.cvssl.Label def) {
        super(controller, parent);
        this.definition = def;
        Integer width = getValueInteger(definition.getWidth(), getObserverWidth());
        if (width == null) width = 10;
        Integer height = getValueInteger(definition.getHeight(), getObserverHeight());
        if (height == null) height = 10;
        Boolean isFill = definition.getIsFill();
        if (isFill == null) isFill = false;
        java.awt.Font font = null;
        java.awt.Color fontColor = null;
        if (definition.getFont() != null) {
            String name = getValueString(definition.getFont().getFontName(), getObserverFontName());
            if (name == null) name = "Arial";
            int style = getFontStyle(definition.getFont());
            Integer size = getValueInteger(definition.getFont().getFontSize(), getObserverFontSize());
            if (size == null) size = 10;
            fontColor = getValueColor(definition.getFont().getFontColor(), getObserverFontColor());
            font = new java.awt.Font(name, style, size);
        }
        String content = getValueString(definition.getText(), getObserverContent());
        widget = new xowl.gmi.view.diagram.Label(width, height, isFill, font, fontColor, content);
    }
    
    private xowl.interpreter.adapter.DataPropertyObserver getObserverWidth() {
        return new xowl.interpreter.adapter.DataPropertyObserver() {
            @Override public void onAdd(Object elem) {
                widget.setWidth((Integer)elem);
                controller.onDiagramUpdated();
            }
            @Override public void onRemove(Object elem) { }
        };
    }
    private xowl.interpreter.adapter.DataPropertyObserver getObserverHeight() {
        return new xowl.interpreter.adapter.DataPropertyObserver() {
            @Override public void onAdd(Object elem) {
                widget.setHeight((Integer)elem);
                controller.onDiagramUpdated();
            }
            @Override public void onRemove(Object elem) { }
        };
    }
    private xowl.interpreter.adapter.DataPropertyObserver getObserverFontName() {
        return new xowl.interpreter.adapter.DataPropertyObserver() {
            @Override public void onAdd(Object elem) {
                widget.setFontName((String)elem);
                controller.onDiagramUpdated();
            }
            @Override public void onRemove(Object elem) { }
        };
    }
    private xowl.interpreter.adapter.DataPropertyObserver getObserverFontSize() {
        return new xowl.interpreter.adapter.DataPropertyObserver() {
            @Override public void onAdd(Object elem) {
                widget.setFontSize((Integer)elem);
                controller.onDiagramUpdated();
            }
            @Override public void onRemove(Object elem) { }
        };
    }
    private xowl.interpreter.adapter.DataPropertyObserver getObserverFontColor() {
        return new xowl.interpreter.adapter.DataPropertyObserver() {
            @Override public void onAdd(Object elem) {
                widget.setFontColor(new java.awt.Color((Integer)elem));
                controller.onDiagramUpdated();
            }
            @Override public void onRemove(Object elem) { }
        };
    }
    private xowl.interpreter.adapter.DataPropertyObserver getObserverContent() {
        return new xowl.interpreter.adapter.DataPropertyObserver() {
            @Override public void onAdd(Object elem) {
                widget.setText((String)elem);
                controller.onDiagramUpdated();
            }
            @Override public void onRemove(Object elem) { }
        };
    }
    
    @Override public xowl.gmi.view.diagram.Widget getWidget() { return widget; }
    
    @Override protected int getContainedASTCount() { return 0; }
    @Override protected int getLeftSiblingsASTCount(Element divider) { return 0; }
    @Override protected boolean insertAST(xowl.gmi.model.ast.ASTNode node) { return false; }
    @Override protected ASTRemovalStatus removeAST(xowl.gmi.model.ast.ASTNode node) { return ASTRemovalStatus.NodeNotFound; }
    
    @Override public void saveTo(xowl.gmi.model.Diagram diagram) { }
    @Override public void decommission() { }
}
